﻿Imports System
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Common
Imports System.Reflection

Public Class frmLogin

    Public Sub initScreen()
        clearLoginScreen()
        loadPreviousLoginId()
    End Sub

    Public Sub clearLoginScreen()
        cboLoginUserName.Text = ""
        txtLoginUserPassword.Text = ""
    End Sub

    Public Sub loadPreviousLoginId()
        Dim dtResult As New DataTable
        Dim lstViewCust As New ListViewItem

        dtResult = findUserLogs()
        cboLoginUserName.Items.Clear()

        If dtResult.Rows.Count > 0 Then
            For Each dataRow As DataRow In dtResult.Rows
                Dim userName As String = dataRow.Item("USER_NAME")

                cboLoginUserName.Items.Add(userName)
            Next dataRow
        End If

        dtResult = getLastLoginId()
        If dtResult.Rows.Count > 0 Then
            Dim dataRow As DataRow = dtResult.Rows(0)
            Dim userName As String = dataRow.Item("USER_NAME")
            cboLoginUserName.Text = userName
        End If
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim dtResult As New DataTable

        dtResult = findUsers(cboLoginUserName.Text, txtLoginUserPassword.Text)

        If dtResult.Rows.Count > 0 Then
            Dim dataRow As DataRow = dtResult.Rows(0)
            _LAST_LOGIN_NAME = cboLoginUserName.Text
            _USER_ID = dataRow.Item("USER_ID")
            _USER_NAME = dataRow.Item("USER_NAME")
            _USER_ROLE = dataRow.Item("USER_ROLE")

            If (Globals.USER_ROLE_WELDER = _USER_ROLE) Then
                Dim welderId As String = dataRow.Item("WELDER_ID")

                dtResult = findProjectWelders(_PROJECT_ID, welderId)

                If dtResult.Rows.Count > 0 Then
                    _FROM_SCREEN_ID = _SCREEN_ID
                    _SCREEN_ID = SCREEN_MENU

                    doCreateLogInLog(_USER_NAME)

                    frmMenu.Show()
                    frmMenu.initScreen()
                    'frmMenu.notifyRejectedWeld()

                    Me.Hide()
                Else
                    MsgBox("You are not involve in this project.", MsgBoxStyle.Exclamation, "Error")
                End If
                
            ElseIf (Globals.USER_ROLE_ENGINEER = _USER_ROLE) Then
                _FROM_SCREEN_ID = _SCREEN_ID
                _SCREEN_ID = SCREEN_MENU

                doCreateLogInLog(_USER_NAME)

                frmMenu.Show()
                frmMenu.initScreen()
                Me.Hide()
            End If

            'frmMenu.manipulateMenuAccessRight()
            'doCreateLogInLog(_USER_NAME)
        Else
            MsgBox("Invalid Username and Password", MsgBoxStyle.Exclamation, "Error")
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class